package com.cy.sys.log.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cy.sys.log.page.vo.JSONResult;
import com.cy.sys.log.service.SysConumerSerivce;
import com.cy.sys.pojo.SysUsersinsert;
/**
 * 	业务呈现数据控制层
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/user/")
public class sysConumercontrller {
	@Autowired
	private SysConumerSerivce sysConumer;
	
	@RequestMapping("doUpdatePassword")
	@ResponseBody
	public JSONResult doupdatepassword(String pwd,String newPwd,String cfgPwd) {
		System.out.println(pwd+"--"+ newPwd+"--"+cfgPwd);
		sysConumer.updatenewpwd(pwd, newPwd, cfgPwd);
		return new JSONResult("update ok");
		
	}
	
	@RequestMapping("doLogin")
	@ResponseBody
	public JSONResult doLogin(String username,String password,boolean isRemeberme){
		
		Subject subject = SecurityUtils.getSubject();
		UsernamePasswordToken token = new UsernamePasswordToken(username, password);
		if(isRemeberme)
			token.setRememberMe(true);
		
		subject.login(token);
		
		return new JSONResult("login ok");
	}
		
	
	@RequestMapping("doFindPageObjects")
	@ResponseBody
	public JSONResult doFindPageObjects(String username,Integer pageCurrent) {
	//	try {	对异常处理的封装使用全局异常处理
		return new JSONResult(sysConumer.findAllUserconsumer(username, pageCurrent));	
//	}catch (Throwable e) {
//		return new JSONResult(e);
//	}
		}
	
	@RequestMapping("doValidById")
	@ResponseBody
	public JSONResult doValidById(Integer id,Integer valid) {
		System.out.println(id+"--"+valid);
		sysConumer.updatestateById(id, valid);
		return new JSONResult("update ok");
	}
	
	
	@RequestMapping("doFindObjectById")
	@ResponseBody
	public JSONResult doUpdateObject(Integer id) {
		//System.out.println(id+"--");
		return new JSONResult(sysConumer.selectfindById(id));
	}
	
	@RequestMapping("doSaveObject")
	@ResponseBody
	public JSONResult doinsertObject(SysUsersinsert sysusers,Integer[] roleIds) {
		sysConumer.userinsertObject(sysusers, roleIds);
		return new JSONResult("insert ok");
	}
	
	//更新
	@RequestMapping("doUpdateObject")
	@ResponseBody
	public JSONResult doSaveUpdateObject(SysUsersinsert sysusers,Integer[] roleIds) {
		sysConumer.UpdateSaveObject(sysusers, roleIds);
		return new JSONResult("update ok");
	}
}
